<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Models\AppVip;
use App\Models\VipOrder;
use Illuminate\Http\Request;

class VipController extends Controller
{

    public function index(Request $Request)
    {
        $data = AppVip::get();
        $auth = AppVip::AUTH;
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'auth'=>$auth]);
    }

    public function isvip(Request $Request)
    {
        //是否是vip
        $vip = VipOrder::with('vip')->where('uid',$Request->uid)->where('status',1)->orderBy('vip_id','desc')->first();
        $data = [];
        if($vip){
            $data = $vip->vip->auth;
            return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
        }else{
            return response()->json(['code' => '201', 'msg' => 'success','data'=>$data]);
        }
        
    }

    public function pay_status(Request $Request)
    {
        $numbers = $Request->numbers;
        if(!$Request->numbers){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = VipOrder::where('numbers', $numbers)->where('status', 1)->first();
        if(empty($data)){
            return response()->json(['code' => '500', 'msg' => '未支付']);
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }
    
    public function order(Request $Request)
    {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = VipOrder::with('user')->when($search, function ($query, $search) {
            return $query->where('due_date', $search);
        });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        foreach ($data as $k => $v){
            $data[$k]->user_name = $v->user->name;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }
}
